local XGJAPI = XGJAPI
function XG_StrReplace_Single(str,i,s)
	return str:sub(1,i-1) .. s .. str:sub(i+1)
end

function XG_StrReplace_Multiple(str,i,j,s)
	return str:sub(1,i-1) .. s .. str:sub(j+1)
end

function XG_StringReplace(str, o, n, c)
    if c >= 0 then
        local head, s = o:sub(1,1), o:sub(2)
        local l, count, al = o:len() - 1, 0, #str
        local d,i = n:len() - l - 1,0
        while i <= al do
            i = i + 1
            if str:sub(i,i) == head then
                if str:sub(i+1,i+l) == s then
                    str = str:sub(1,i-1) .. n .. str:sub(i+l+1)
                    al = al + d
                    count=count+1
                    if count==c then
                        break
                    end
                end
            end
        end
        return str
    end

    return str:gsub(o, n)
end

Xfunc['XG_StrReplace_Single']=function ()
    XGJAPI.string[0] = XG_StrReplace_Single(
        XGJAPI.string[1] or '',
        XGJAPI.integer[2] or 0,
        XGJAPI.string[3] or ''
    )
end

Xfunc['XG_StrReplace_Multiple']=function ()
    XGJAPI.string[0] = XG_StrReplace_Multiple(
        XGJAPI.string[1] or '',
        XGJAPI.integer[2] or 0,
        XGJAPI.integer[3] or 0,
        XGJAPI.string[4] or ''
    )
end

Xfunc['XG_StringReplace']=function ()
    XGJAPI.string[0] = XG_StringReplace(
        XGJAPI.string[1] or '',
        XGJAPI.string[2] or '',
        XGJAPI.string[3] or '',
        XGJAPI.integer[4] or 0
    )
end
